<div class="container">
    <h1 id="pkg-overview">package rand</h1>
    <p><code>import "math/rand"</code>
    </p><p align="left">rand包实现了伪随机数生成器。</p>
    <p align="left">随机数从资源生成。包水平的函数都使用的默认的公共资源。该资源会在程序每次运行时都产生确定的序列。如果需要每次运行产生不同的序列，应使用Seed函数进行初始化。默认资源可以安全的用于多go程并发。</p>
    <div class="panel-group">
        <div class="panel panel-default" id="example-package">
            <div class="panel-heading" onclick="document.getElementById('ex-package').style.display = document.getElementById('ex-package').style.display=='none'?'block':'none';">Example</div>
            <div class="panel-collapse collapse" id="ex-package">
                <div class="panel-body">
                    <pre>rand.Seed(42) <span class="com">// Try changing this number!</span>
answers := []string{
    "It is certain",
    "It is decidedly so",
    "Without a doubt",
    "Yes definitely",
    "You may rely on it",
    "As I see it yes",
    "Most likely",
    "Outlook good",
    "Yes",
    "Signs point to yes",
    "Reply hazy try again",
    "Ask again later",
    "Better not tell you now",
    "Cannot predict now",
    "Concentrate and ask again",
    "Don't count on it",
    "My reply is no",
    "My sources say no",
    "Outlook not so good",
    "Very doubtful",
}
fmt.Println("Magic 8-Ball says:", answers[rand.Intn(len(answers))])</pre>
                    <p>Output:
                    </p><pre>Magic 8-Ball says: As I see it yes
</pre>
                </div>
            </div>
        </div>
        <div class="panel panel-default" id="example-package--Rand">
            <div class="panel-heading" onclick="document.getElementById('ex-package--Rand').style.display = document.getElementById('ex-package--Rand').style.display=='none'?'block':'none';">Example (Rand)</div>
            <div class="panel-collapse collapse" id="ex-package--Rand">
                <div class="panel-body">
                    <pre><span class="com">// Create and seed the generator.</span>
<span class="com">// Typically a non-fixed seed should be used, such as time.Now().UnixNano().</span>
<span class="com">// Using a fixed seed will produce the same output on every run.</span>
r := rand.New(rand.NewSource(99))
<span class="com">// The tabwriter here helps us generate aligned output.</span>
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
defer w.Flush()
show := func(name string, v1, v2, v3 interface{}) {
    fmt.Fprintf(w, "%s\t%v\t%v\t%v\n", name, v1, v2, v3)
}
<span class="com">// Float32 and Float64 values are in [0, 1).</span>
show("Float32", r.Float32(), r.Float32(), r.Float32())
show("Float64", r.Float64(), r.Float64(), r.Float64())
<span class="com">// ExpFloat64 values have an average of 1 but decay exponentially.</span>
show("ExpFloat64", r.ExpFloat64(), r.ExpFloat64(), r.ExpFloat64())
<span class="com">// NormFloat64 values have an average of 0 and a standard deviation of 1.</span>
show("NormFloat64", r.NormFloat64(), r.NormFloat64(), r.NormFloat64())
<span class="com">// Int31, Int63, and Uint32 generate values of the given width.</span>
<span class="com">// The Int method (not shown) is like either Int31 or Int63</span>
<span class="com">// depending on the size of 'int'.</span>
show("Int31", r.Int31(), r.Int31(), r.Int31())
show("Int63", r.Int63(), r.Int63(), r.Int63())
show("Uint32", r.Int63(), r.Int63(), r.Int63())
<span class="com">// Intn, Int31n, and Int63n limit their output to be &lt; n.</span>
<span class="com">// They do so more carefully than using r.Int()%n.</span>
show("Intn(10)", r.Intn(10), r.Intn(10), r.Intn(10))
show("Int31n(10)", r.Int31n(10), r.Int31n(10), r.Int31n(10))
show("Int63n(10)", r.Int63n(10), r.Int63n(10), r.Int63n(10))
<span class="com">// Perm generates a random permutation of the numbers [0, n).</span>
show("Perm", r.Perm(5), r.Perm(5), r.Perm(5))</pre>
                    <p>Output:
                    </p><pre>Float32     0.2635776           0.6358173           0.6718283
Float64     0.628605430454327   0.4504798828572669  0.9562755949377957
ExpFloat64  0.3362240648200941  1.4256072328483647  0.24354758816173044
NormFloat64 0.17233959114940064 1.577014951434847   0.04259129641113857
Int31       1501292890          1486668269          182840835
Int63       3546343826724305832 5724354148158589552 5239846799706671610
Uint32      5927547564735367388 637072299495207830  4128311955958246186
Intn(10)    1                   2                   5
Int31n(10)  4                   7                   8
Int63n(10)  7                   6                   3
Perm        [1 4 2 3 0]         [4 2 1 3 0]         [1 2 4 0 3]
</pre>
                </div>
            </div>
        </div>
    </div>
    
    
		
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
    
		
        
        
    
    <h2 id="Source">type <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#18" title="View Source">Source</a> </h2>
    <pre>type Source interface {
    <span id="Source.Int63">Int63</span>() <a href="builtin.htm#int64">int64</a>
    <span id="Source.Seed">Seed</span>(seed <a href="builtin.htm#int64">int64</a>)
}</pre>
    <p>Source代表一个生成均匀分布在范围[0, 1&lt;&lt;63)的int64值的（伪随机的）资源。</p>
    <h3 id="NewSource">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#24" title="View Source">NewSource</a> </h3>
    <pre class="funcdecl">func NewSource(seed <a href="builtin.htm#int64">int64</a>) <a href="#Source">Source</a></pre>
    <p>使用给定的种子创建一个伪随机资源。</p>
    <h2 id="Rand">type <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#31" title="View Source">Rand</a> </h2>
    <pre>type Rand struct {
    <span class="com">// 内含隐藏或非导出字段</span>
}</pre>
    <p>Rand生成服从多种分布的随机数。</p>
    <h3 id="New">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#37" title="View Source">New</a> </h3>
    <pre class="funcdecl">func New(src <a href="#Source">Source</a>) *<a href="#Rand">Rand</a></pre>
    <p>返回一个使用src生产的随机数来生成其他各种分布的随机数值的*Rand。</p>
    <h3 id="Rand.Seed">func (*Rand) <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#40" title="View Source">Seed</a> </h3>
    <pre class="funcdecl">func (r *<a href="#Rand">Rand</a>) Seed(seed <a href="builtin.htm#int64">int64</a>)</pre>
    <p>使用给定的seed来初始化生成器到一个确定的状态。</p>
    <h3 id="Rand.Int">func (*Rand) <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#52" title="View Source">Int</a> </h3>
    <pre class="funcdecl">func (r *<a href="#Rand">Rand</a>) Int() <a href="builtin.htm#int">int</a></pre>
    <p>返回一个非负的伪随机int值。</p>
    <h3 id="Rand.Int31">func (*Rand) <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#49" title="View Source">Int31</a> </h3>
    <pre class="funcdecl">func (r *<a href="#Rand">Rand</a>) Int31() <a href="builtin.htm#int32">int32</a></pre>
    <p>返回一个int32类型的非负的31位伪随机数。</p>
    <h3 id="Rand.Int63">func (*Rand) <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#43" title="View Source">Int63</a> </h3>
    <pre class="funcdecl">func (r *<a href="#Rand">Rand</a>) Int63() <a href="builtin.htm#int64">int64</a></pre>
    <p>返回一个int64类型的非负的63位伪随机数。</p>
    <h3 id="Rand.Uint32">func (*Rand) <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#46" title="View Source">Uint32</a> </h3>
    <pre class="funcdecl">func (r *<a href="#Rand">Rand</a>) Uint32() <a href="builtin.htm#uint32">uint32</a></pre>
    <p>返回一个uint32类型的非负的32位伪随机数。</p>
    <h3 id="Rand.Intn">func (*Rand) <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#93" title="View Source">Intn</a> </h3>
    <pre class="funcdecl">func (r *<a href="#Rand">Rand</a>) Intn(n <a href="builtin.htm#int">int</a>) <a href="builtin.htm#int">int</a></pre>
    <p>返回一个取值范围在[0,n)的伪随机int值，如果n&lt;=0会panic。</p>
    <h3 id="Rand.Int31n">func (*Rand) <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#76" title="View Source">Int31n</a> </h3>
    <pre class="funcdecl">func (r *<a href="#Rand">Rand</a>) Int31n(n <a href="builtin.htm#int32">int32</a>) <a href="builtin.htm#int32">int32</a></pre>
    <p>返回一个取值范围在[0,n)的伪随机int32值，如果n&lt;=0会panic</p>
    <h3 id="Rand.Int63n">func (*Rand) <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#59" title="View Source">Int63n</a> </h3>
    <pre class="funcdecl">func (r *<a href="#Rand">Rand</a>) Int63n(n <a href="builtin.htm#int64">int64</a>) <a href="builtin.htm#int64">int64</a></pre>
    <p>返回一个取值范围在[0,n)的伪随机int64值，如果n&lt;=0会panic。</p>
    <h3 id="Rand.Float32">func (*Rand) <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#131" title="View Source">Float32</a> </h3>
    <pre class="funcdecl">func (r *<a href="#Rand">Rand</a>) Float32() <a href="builtin.htm#float32">float32</a></pre>
    <p>返回一个取值范围在[0.0, 1.0)的伪随机float32值。</p>
    <h3 id="Rand.Float64">func (*Rand) <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#104" title="View Source">Float64</a> </h3>
    <pre class="funcdecl">func (r *<a href="#Rand">Rand</a>) Float64() <a href="builtin.htm#float64">float64</a></pre>
    <p>返回一个取值范围在[0.0, 1.0)的伪随机float64值。</p>
    <h3 id="Rand.NormFloat64">func (*Rand) <a href="https://github.com/golang/go/blob/master/src/math/rand/normal.go?name=release#38" title="View Source">NormFloat64</a> </h3>
    <pre class="funcdecl">func (r *<a href="#Rand">Rand</a>) NormFloat64() <a href="builtin.htm#float64">float64</a></pre>
    <p align="left">返回一个服从标准正态分布（标准差=1，期望=0）、取值范围在[-math.MaxFloat64, +math.MaxFloat64]的float64值</p>
    <p align="left">如果要生成不同的正态分布值，调用者可用如下代码调整输出：</p>
    <pre>sample = NormFloat64() * 标准差 + 期望</pre>
    <h3 id="Rand.ExpFloat64">func (*Rand) <a href="https://github.com/golang/go/blob/master/src/math/rand/exp.go?name=release#31" title="View Source">ExpFloat64</a> </h3>
    <pre class="funcdecl">func (r *<a href="#Rand">Rand</a>) ExpFloat64() <a href="builtin.htm#float64">float64</a></pre>
    <p align="left">返回一个服从标准指数分布（率参数=1，率参数是期望的倒数）、取值范围在(0, +math.MaxFloat64]的float64值</p>
    <p align="left">如要生成不同的指数分布值，调用者可用如下代码调整输出：</p>
    <pre>sample = ExpFloat64() / 率参数
</pre>
    <h3 id="Rand.Perm">func (*Rand) <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#146" title="View Source">Perm</a> </h3>
    <pre class="funcdecl">func (r *<a href="#Rand">Rand</a>) Perm(n <a href="builtin.htm#int">int</a>) []<a href="builtin.htm#int">int</a></pre>
    <p>返回一个有n个元素的，[0,n)范围内整数的伪随机排列的切片。</p>
    <h2 id="Zipf">type <a href="https://github.com/golang/go/blob/master/src/math/rand/zipf.go?name=release#15" title="View Source">Zipf</a> </h2>
    <pre>type Zipf struct {
    <span class="com">// 内含隐藏或非导出字段</span>
}</pre>
    <p>Zipf生成服从齐普夫分布的随机数。</p>
    <h3 id="NewZipf">func <a href="https://github.com/golang/go/blob/master/src/math/rand/zipf.go?name=release#37" title="View Source">NewZipf</a> </h3>
    <pre class="funcdecl">func NewZipf(r *<a href="#Rand">Rand</a>, s <a href="builtin.htm#float64">float64</a>, v <a href="builtin.htm#float64">float64</a>, imax <a href="builtin.htm#uint64">uint64</a>) *<a href="#Zipf">Zipf</a></pre>
    <p align="left">NewZipf返回一个[0, imax]范围内的齐普夫随机数生成器。</p>
    <p align="left">齐普夫分布：值k出现的几率p(k)正比于(v+k)**(-s)，其中s&gt;1且k&gt;=0且v&gt;=1。
</p>
    <h3 id="Zipf.Uint64">func (*Zipf) <a href="https://github.com/golang/go/blob/master/src/math/rand/zipf.go?name=release#56" title="View Source">Uint64</a> </h3>
    <pre class="funcdecl">func (z *<a href="#Zipf">Zipf</a>) Uint64() <a href="builtin.htm#uint64">uint64</a></pre>
    <p>Uint64返回一个服从Zipf对象描述的齐普夫分布的随机数。</p>
    <h2 id="Seed">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#165" title="View Source">Seed</a> </h2>
    <pre class="funcdecl">func Seed(seed <a href="builtin.htm#int64">int64</a>)</pre>
    <p>使用给定的seed将默认资源初始化到一个确定的状态；如未调用Seed，默认资源的行为就好像调用了Seed(1)。</p>
    <h2 id="Int">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#180" title="View Source">Int</a> </h2>
    <pre class="funcdecl">func Int() <a href="builtin.htm#int">int</a></pre>
    <p>返回一个非负的伪随机int值。</p>
    <h2 id="Int31">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#177" title="View Source">Int31</a> </h2>
    <pre class="funcdecl">func Int31() <a href="builtin.htm#int32">int32</a></pre>
    <p>返回一个int32类型的非负的31位伪随机数。</p>
    <h2 id="Int63">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#169" title="View Source">Int63</a> </h2>
    <pre class="funcdecl">func Int63() <a href="builtin.htm#int64">int64</a></pre>
    <p>返回一个int64类型的非负的63位伪随机数。</p>
    <h2 id="Uint32">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#173" title="View Source">Uint32</a> </h2>
    <pre class="funcdecl">func Uint32() <a href="builtin.htm#uint32">uint32</a></pre>
    <p>返回一个uint32类型的非负的32位伪随机数。</p>
    <h2 id="Intn">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#195" title="View Source">Intn</a> </h2>
    <pre class="funcdecl">func Intn(n <a href="builtin.htm#int">int</a>) <a href="builtin.htm#int">int</a></pre>
    <p>返回一个取值范围在[0,n)的伪随机int值，如果n&lt;=0会panic。</p>
    <h2 id="Int31n">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#190" title="View Source">Int31n</a> </h2>
    <pre class="funcdecl">func Int31n(n <a href="builtin.htm#int32">int32</a>) <a href="builtin.htm#int32">int32</a></pre>
    <p>返回一个取值范围在[0,n)的伪随机int32值，如果n&lt;=0会panic。</p>
    <h2 id="Int63n">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#185" title="View Source">Int63n</a> </h2>
    <pre class="funcdecl">func Int63n(n <a href="builtin.htm#int64">int64</a>) <a href="builtin.htm#int64">int64</a></pre>
    <p>返回一个取值范围在[0, n)的伪随机int64值，如果n&lt;=0会panic。</p>
    <h2 id="Float32">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#203" title="View Source">Float32</a> </h2>
    <pre class="funcdecl">func Float32() <a href="builtin.htm#float32">float32</a></pre>
    <p>返回一个取值范围在[0.0, 1.0)的伪随机float32值。</p>
    <h2 id="Float64">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#199" title="View Source">Float64</a> </h2>
    <pre class="funcdecl">func Float64() <a href="builtin.htm#float64">float64</a></pre>
    <p>返回一个取值范围在[0.0, 1.0)的伪随机float64值。</p>
    <h2 id="NormFloat64">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#218" title="View Source">NormFloat64</a> </h2>
    <pre class="funcdecl">func NormFloat64() <a href="builtin.htm#float64">float64</a></pre>
    <p align="left">返回一个服从标准正态分布（标准差=1，期望=0）、取值范围在[-math.MaxFloat64, +math.MaxFloat64]的float64值</p>
    <p align="left">如果要生成不同的正态分布值，调用者可用如下代码调整输出：</p>
    <pre>sample = NormFloat64() * 标准差 + 期望
</pre>
    <h2 id="ExpFloat64">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#228" title="View Source">ExpFloat64</a> </h2>
    <pre class="funcdecl">func ExpFloat64() <a href="builtin.htm#float64">float64</a></pre>
    <p align="left">返回一个服从标准指数分布（率参数=1，率参数是期望的倒数）、取值范围在(0, +math.MaxFloat64]的float64值</p>
    <p align="left">如要生成不同的指数分布值，调用者可用如下代码调整输出：</p>
    <pre>sample = ExpFloat64() / 率参数</pre>
    <h2 id="Perm">func <a href="https://github.com/golang/go/blob/master/src/math/rand/rand.go?name=release#207" title="View Source">Perm</a> </h2>
    <pre class="funcdecl">func Perm(n <a href="builtin.htm#int">int</a>) []<a href="builtin.htm#int">int</a></pre>
    <p>返回一个有n个元素的，[0,n)范围内整数的伪随机排列的切片。</p>
</div>